package org.learn.intermediate.houserent.service;

import org.learn.intermediate.houserent.domain.House;

public class HomeService {
    private House[] houses ;//保存house对象
    private int houseNums =1;//记录有多少个房屋信息
    private  int idCounter = 1;//记录id自增长到哪个值；
    //构造器
    public HomeService(int size) {
        //new houses
        houses = new House[size];
     //配合测试列表，初始化一个houses对象

        houses[0] = new House(1,"jack","142423","中国",1000,"未出租");

    }
    //list方法
    public House[] list() {
        return  houses;
    }

    //del方法，删除一个房屋信息
    public boolean del(int deild) {
        int index = -1;
        for (int i = 0; i < houseNums; i++) {
            if (deild == houses[i].getId()) {
                index = i;
            }
        }
        if(index == -1){
            return false;
        }
        for(int i = index;i < houseNums - 1;i++){
            houses[i] = houses[i+1];
        }
        houses[--houseNums-1] = null;
        return true;
    }
    //add方法，添加新对象，返回boolean
    public boolean add(House newHouse) {
        //判断是否还能继续添加
        if(houseNums == houses.length) {
            System.out.println("数组已满，不能再添加了");
            return false;
        }
        //新增一个房屋
        houses[houseNums++] = newHouse;
       //自增长机制
        newHouse.setId(++idCounter);
        return true;

    }
}
